﻿using System;
using System.Collections.Generic;
using System.Web.Http;
using System.Web.Http.Filters;
using HotelRegistrationSystem.DAL;
using HotelRegistrationSystem.Models;
using Newtonsoft.Json;
using System.Net.Http;
using System.Net;

namespace HotelRegistrationSystem.Controllers
{
    [AllowAnonymous]
    [AllowCrossSiteJson]
    public class HotelApiController : ApiController
    {
        public HttpResponseMessage Get()
        {
            HttpResponseMessage msg = null;
            List<HotelIndexPageModel> hotels = HotelDAL.GetHotels();
            if (hotels == null)
            {
                msg = Request.CreateResponse(HttpStatusCode.NotFound, "Hotels are not found");
            }
            else
            {
                msg = Request.CreateResponse(HttpStatusCode.OK, hotels);
            }
            return msg;
        }

        public HttpResponseMessage Get(int id)
        {
            HttpResponseMessage msg = null;
            Hotel hotel = HotelDAL.GetHotel(id);
            if (hotel == null)
            {
                msg = Request.CreateResponse(HttpStatusCode.NotFound, "Hotel not found");
            }
            else
            {
                HotelViewModel result = hotel.ToHotelViewModel();
                msg = Request.CreateResponse(HttpStatusCode.OK, result);
            }

            return msg;
        }

        public HttpResponseMessage Post([FromBody]string value)
        {
            //var hotel = JsonConvert.DeserializeObject<CreateHotelModel>(value);
            //hotel.AddressId = 2;
            //hotel.HotelTypeId = 1;
            //hotel.ReservationTypeId = 1;
            //hotel.UserId = 1;
            //HotelDAL.AddHotel(hotel);
            var msg = Request.CreateResponse(HttpStatusCode.Created);
            //msg.Headers.Location = new Uri(Request.RequestUri + hotel.HotelTypeId.ToString());
            return msg;
        }

        public HttpResponseMessage Put(int id, [FromBody]string value)
        {
            var hotel = JsonConvert.DeserializeObject<Hotel>(value);
            hotel.UserId = 1;
            HotelDAL.EditHotel(hotel.ToHotelViewModel());

            var msg = Request.CreateResponse(HttpStatusCode.OK);
            return msg;
        }

        public HttpResponseMessage Delete(int id)
        {
            HotelDAL.RemoveHotel(id);
            var msg = Request.CreateResponse(HttpStatusCode.OK);
            return msg;
        }
    }
    
    public class AllowCrossSiteJsonAttribute : ActionFilterAttribute
    {
        public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
        {
            if (actionExecutedContext.Response != null)
                actionExecutedContext.Response.Headers.Add("Access-Control-Allow-Origin", "*");

            base.OnActionExecuted(actionExecutedContext);
        }
    }
    
    public static class HotelExt
    {
        public static HotelViewModel ToHotelViewModel(this Hotel hotel)
        {
            HotelViewModel result = result = new HotelViewModel
            {
                HotelId = hotel.HotelId,
                Name = hotel.Name,
                EarliestCheckIn = hotel.EarliestCheckIn,
                LatestCheckOut = hotel.LatestCheckOut,
                Address = new AddressModel
                {
                    AddressId = hotel.Address.AddressId,
                    Region = hotel.Address.Region,
                    PostCode = hotel.Address.PostCode,
                    Country = hotel.Address.Country,
                    City = hotel.Address.City,
                    Street = hotel.Address.Street
                },
                PhoneNumber = hotel.PhoneNumber,
                Email = hotel.Email,
                HotelTypeId = hotel.HotelType.HotelTypeId,
                HotelDescription = hotel.Description,
                Earnest = hotel.Earnest,
                ReservationDenyPeriod = hotel.ReservationDenyPeriod,
                ContactPeriod = hotel.ContactPeriod,
                ReservationTypeId = hotel.ReservationType.ReservationTypeId,
                Stars = hotel.Stars,
                PictureName = ""
            };

            return result;
        }
    }
}